//
//  ZBVisitorLogin.swift
//  ZBWb
//
//  Created by 天隐翎 on 16/4/8.
//  Copyright © 2016年 csdn. All rights reserved.
//

import UIKit
import SnapKit

//定义协议
@objc protocol ZBVisitorLoginDelegate:NSObjectProtocol{
    
   optional func userWilllogin()
    
   optional func userWillRegiser()
}
class ZBVisitorLogin: UIView {
    
//    设置属性
    weak var visitorDelegate:ZBVisitorLoginDelegate?
//    重写init方法,把控件加载进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
//        图片旋转动画
        imageAnimationStart()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    向外界设置一个方法,并设置参数,传递内容,来设置
    func setVisitorInto(tipText:String ,imagename:String ,isHome: Bool = false){
        if isHome{
            imageAnimationStart()
        }else{
            
            bigHouse.hidden = true
            
//            将圆圈移动到最上边
            bringSubviewToFront(circleIcon)
        }
        wenLable.text = tipText
        circleIcon.image = UIImage(named: imagename)
      
    }
//    图片动画旋转
    private func imageAnimationStart() {
        let animat = CABasicAnimation(keyPath: "transform.rotation")
//        设置旋转数
        animat.repeatCount = MAXFLOAT
//        设置时间(转一圈)
        animat.duration = 15
//        设置旋转度数
        animat.toValue = 2 * M_PI
        
        animat.removedOnCompletion = false
//        设置动画
        circleIcon.layer.addAnimation(animat, forKey: nil)
    }
//    添加子空间,并设置布局
    private func setUI(){
        
        addSubview(circleIcon)
        addSubview(bigHouse)
        addSubview(wenLable)
        addSubview(dengbtn)
        addSubview(zhubtn)
        addSubview(backimage)
//        转图布局
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
//        房子图片布局
        bigHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
//        文字布局
        wenLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
        }
//        登录布局
        dengbtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(wenLable.snp_left)
            make.top.equalTo(wenLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
//        注册布局
        zhubtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(wenLable.snp_right)
            make.top.equalTo(wenLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
//        背景布局
        backimage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
//        设置大背景
//        let color = CGFloat(237/255.0)
        let color:CGFloat = 237/255.0
        
        backgroundColor = UIColor(red: color, green: color, blue: color, alpha: 1)
        
        
//        设置点击事件
        dengbtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        zhubtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
        
    }
//    点击事件
    @objc private func loginBtnDidClick(){
//        使用代理调用代理方法
        visitorDelegate?.userWilllogin!()
    }
    
    @objc private func registerBtnDidClick(){
//        使用代理调用代理方法
        visitorDelegate?.userWillRegiser!()
    }
//    懒加载控件
//    背景图
    private lazy var backimage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
//    圆圈
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
//    大房子
    private lazy var bigHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//    提示文字
    private lazy var wenLable: UILabel = UILabel(text: "关注一些人", textColor: UIColor.darkGrayColor(), fontSize: 14)
    
//    private lazy var wenLable:UILabel = {
//        let la = UILabel()
//        
//        
//        la.text = "关注一些人,回到这里看看有什么惊喜"
//        la.textColor = UIColor.darkGrayColor()
//        la.font = UIFont.systemFontOfSize(14)
//        la.numberOfLines = 0
//        la.textAlignment = .Center
////        自适应大小
//        la.sizeToFit()
//        return la
//        
//    }()
//    登录按钮
    private lazy var dengbtn: UIButton = {
        let deng = UIButton()
        deng.setTitle("登录", forState: .Normal)
        deng.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        deng.titleLabel?.font = UIFont.systemFontOfSize(14)
//        拉伸背景图片
        let image = UIImage(named: "common_button_white_disable")!
        let width = Int(image.size.width*0.5)
        let heigh = Int(image.size.height*0.5)
        
//        设置背景图片
        deng.setBackgroundImage(image.stretchableImageWithLeftCapWidth(width, topCapHeight: heigh), forState: .Normal)
        return deng
    }()
//    注册按钮
    private lazy var zhubtn: UIButton = {
        let zhu = UIButton()
        zhu.setTitle("注册", forState: .Normal)
        zhu.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        zhu.titleLabel?.font = UIFont.systemFontOfSize(14)
//        拉伸
        let image = UIImage(named: "common_button_white_disable")!
        let width = Int(image.size.width*0.5)
        let heiht = Int(image.size.height*0.5)
        zhu.setBackgroundImage(image.stretchableImageWithLeftCapWidth(width, topCapHeight: heiht), forState: .Normal)
//        设置背景图片
//        zhu.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return zhu
    
    }()
    

}
